﻿using System.Collections.Generic;
using Systems.SaveSystem;
using UnityEngine;

public abstract class GuidSaveCompAbs : MonoBehaviour
{
    private GuidComponent guidComponent;
    public string guid;


    public void Start()
    {
        guidComponent = transform.GetComponent<GuidComponent>();
        if (guidComponent == null)
        {
            //中断游戏
            Debug.LogError("GuidComponent is null");
            Application.Quit();
        }

        guid = guidComponent.GetGuid().ToString();
        // 重新注册 确保没有空引用
        GuidSaveManager.Instance.RegisterSaveable(this);
        // 加载数据
        if (GuidSaveManager.Instance.sceneData.ContainsKey(guid))
        {
            Dictionary<string,object> data = GuidSaveManager.Instance.sceneData[guid];
            if (data == null)
            {
                // 说明Save方法没有放任何东西或没有存储过
            }
            else
            {
                // 包含死亡逻辑
                if (data.ContainsKey("Dead"))
                {
                    Destroy(gameObject);
                }
                else
                {
                    LoadData(data);
                }
            }
        }
        else
        {
            //说明还没注册过
        }
    }

    public abstract Dictionary<string, object> SaveData();

    public abstract void LoadData(Dictionary<string, object> data);
}